import 'package:get/get.dart';
import 'package:xin_he/page/login/model/xin_he_user_info_entity.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_toast.dart';
import 'xin_he_register_state.dart';
import 'package:get/get.dart'  hide MultipartFile, FormData;
import 'package:dio/dio.dart';
class XinHeRegisterLogic extends GetxController {
  final XinHeRegisterState state = XinHeRegisterState();

  Future<bool> clickGetCode() async {
    // if(state.name.isEmpty){
    //   XinHeToast.center('请输入姓名');
    //   return false;
    // }

    if (state.phone.isEmpty) {
      XinHeToast.center('请输入手机号');
      return false;
    }
    if (state.phone.length!=(11+2)) {
      XinHeToast.center('请输入11位手机号');
      return false;
    }
    if(!GetUtils.isPhoneNumber(state.phone.replaceAll(' ', ''))){
      XinHeToast.center('手机号格式错误!');
      return false;
    }

    Map<String,dynamic>map = {
      'mobile':state.phone.replaceAll(' ', ''),

    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/alibaba/v2/sendVerifyCodeByMobile',params: map,isPost: true,isPostAndwwwForm: true);



    return true;
  }

  /// 回收键盘
  void keyboardBack() {
    if (Get.focusScope?.hasPrimaryFocus == true) {
      Get.focusScope?.unfocus();
    }
  }

  ///是否勾选
  changeSelectedStatus() {
    state.selected.value = !state.selected.value;
    setSaveButtonStatus();
  }


  ///手机号输入框-输入值改变回调
  void onPhoneChange(String value){
    state.phone = value;
    setSaveButtonStatus();
    if(state.phone.length !=(11+2)){
      state.codeEnable.value = false;
    }else {
      state.codeEnable.value = true;

    }

  }

  ///密码
  void onPwChang(String str) {
    state.pw = str;
    setSaveButtonStatus();

  }


  /// 验证码
  void onCodeChange(String str) {
    state.code = str;
    setSaveButtonStatus();
  }

  ///邀请码
  void onInviteCodeChange(String str){
    state.inviteCode.value = str;
    // setSaveButtonStatus();
  }


  ///OCR扫描
  void onOcrAction(){

  }

  ///设置状态
  setSaveButtonStatus() {
    if( state.phone.length!=(11+2) || state.code.length <6 || state.selected.value == false || state.name.isEmpty){
      state.saveEnable.value = false;
    }else{
      state.saveEnable.value = true;
    }
  }

  void onNameChange(String text){
    state.name = text;
    setSaveButtonStatus();
  }


  onRegisterAction()async {
    Map<String,dynamic>map = {
      'mobile':state.phone.replaceAll(' ', ''),
      'code':'123123',
      'name':state.name,
      'parentCode':'15816899401'

    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/member/appRegister',params: map,isPost: false);

    if(!result.result){

      return ;
    }
    if(result.data['code'] == 0){
      Map<String, dynamic> data = result.data['data'];
      XinHeUserInfoEntity model = XinHeUserInfoEntity.fromJson(data);
      XinHeToast.center('注册成功，请重新登录');
      Get.back();
    }else {

    }



  }

  registerAction() async{


  }

}
